package br.com.br1.fv.mobile;

import java.util.Collection;

import org.droidsql.DAO;
import org.droidsql.DBHelpper;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import br.com.br1.fv.db.DBInitializer;
import br.com.br1.fv.db.entities.Cliente;

public class ListaClientesActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		DBHelpper.initSingleton(this, new DBInitializer());
		
		TabHost tabHost = getTabHost();

		LayoutInflater.from(this).inflate(R.layout.activity_clientes, tabHost.getTabContentView(), true);

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Sequencia de visitas").setContent(R.id.listView1));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Lista completa").setContent(R.id.listView2));

		ListView seqVisita = (ListView) findViewById(R.id.listView1);
		
		DAO<Cliente> dao = new DAO<Cliente>(Cliente.class);
		Collection<Cliente> clientes = null;
		try{
			clientes = dao.list(null, null);
			ListAdapter adapter = new ClienteListAdapter(this, android.R.layout.simple_list_item_1, clientes.toArray(new Cliente[clientes.size()]));
			seqVisita.setAdapter(adapter);
			seqVisita.setOnItemClickListener(new OnItemClickListener(){
				public void onItemClick(AdapterView<?> av, View v, int pos,	long id) {
					Cliente c = (Cliente) v.getTag(R.id.cadastroClientes);
					cadastroCliente(c._id);
				}});
			
		} catch (Throwable t){
			throw new IllegalStateException(t);
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lista_clientes, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		cadastroCliente(-1);
		return true;
	}
	
	public void cadastroCliente(int cliente){
		Intent intent = new Intent().setClass(this, ClienteActivity.class);
		intent.putExtra("cliente", cliente);
		startActivity(intent);
	}
	
	public class ClienteListAdapter extends ArrayAdapter<Cliente>{
		
		Context context;
		int layoutResourceId;   
		Cliente data[] = null;
		
		public ClienteListAdapter(Context context, int layoutResourceId, Cliente[] data) {
			super(context, layoutResourceId, data);
			this.layoutResourceId = layoutResourceId;
			this.context = context;
			this.data = data;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView row = (TextView) convertView;
			
			if(row == null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				row = (TextView) inflater.inflate(layoutResourceId, parent, false);
			}
			
			Cliente itemVO = data[position];
			row.setText(itemVO.nome);
			row.setTag(R.id.cadastroClientes, itemVO);
			
			return row;
		}
	}
}

